import datetime
import time

import requests
import json

url = "https://crm.hongsong.info/crmapi/api/edu-fengjin-admin-web/chart/chatMessages"

current = 1
message_datas = []
while True:
    payload = json.dumps({
        "data": {
            "current": current,
            "pageSize": 100,
            "roundId": 106962,
            "roomId": "r_kfhlt19fsg0244",
            "lastSequenceId": 1722510656837000,
            "desc": True
        }
    })
    headers = {
        'istio-v': 'feature-liveclub0731',
        'loginsource': 'crm',
        'pragma': 'no-cache',
        'priority': 'u=1, i',
        'token': 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJmYW5jaGFvQGhvbmdzb25nLmNsdWIiLCJjcmVhdGVkIjoxNzIxNzMzODk2MjEzLCJleHAiOjE3MjQzNjA0NTZ9.01eogZ_ieALmIsmRrrR5zpfqXBNijRNQ7kK7hU9-nJAQhGaM1PrXyv_WP4DhBsMcomOQYNe_JHqyj71DMjkjJw',
        'userid': 'null',
        'x-hs-employee-token': '[object Object]',
        'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
        'content-type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    current += 1
    message_datas.extend(response.json()['data']['data'])
    print(len(response.json()['data']['data']))
    print(response.json()['data']['data'][-1]['sendTime'])

    print('当前时间', time.strftime("%Y-%m-%d %H:%M:%S"))

    now = datetime.datetime.now()
    two_minutes_ago = now - datetime.timedelta(seconds=120)
    print(two_minutes_ago.strftime("%Y-%m-%d %H:%M:%S"))

    if two_minutes_ago.strftime("%Y-%m-%d %H:%M:%S") < response.json()['data']['data'][-1]['sendTime']:
        print('本次请求的最后一条的消息的发送时间不在两分钟前')
    else:
        print('本次请求的最后一条的消息的发送时间在两分钟前，停止循环')
        break

print(f'总共{len(message_datas)}条数据')
print(message_datas)
